/* 
 *  This file is part of hmvcBlog-blog.
 *
 *  hmvcBlog-blog is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  hmvcBlog-blog is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU Affero General Public License for more details.
 *
 *  You should have received a copy of the GNU Affero General Public License
 *  along with hmvcBlog-blog.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
package com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.login;

import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.authentification.client.login.events.in.ShowLoginViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.client.applicationframe.events.in.AddApplicationViewEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.historytoken.client.HistoryToken;
import com.googlecode.hmvc4gwt.example.hmvcblog.frontend.i18n.client.I18N;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.dto.user.UserLoginDTO;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.LoginEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.LoginNotFoundEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.PasswordEmptyEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.login.in.LoginEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.login.out.LoginSuccessfulEvent;
import com.googlecode.hmvc4gwt.example.hmvcblog.service.gwtrpc.client.user.events.login.out.PasswordWrongEvent;
import com.googlecode.hmvc4gwt.framework.client.rpc.ASessionAsyncCallback;
import com.googlecode.hmvc4java.event.IEvent;
import com.googlecode.hmvc4java.registry.controller.AControllerViewRegistry;

/**
 * 
 * 
 * @author jens.meiss
 */
public class LoginController extends AControllerViewRegistry<LoginView> {

	/**
	 * Setzt die View zusammen
	 */
	private void buildView() {

		VerticalPanel verticalPanel = new VerticalPanel();
		verticalPanel.add(getView().getLabelDescription());
		verticalPanel.add(getView().getGridLoginBox());

		Grid gridContainer = getView().getGridContainer();
		gridContainer.setWidget(0, 0, verticalPanel);

	}

	private void disableButtonLoging() {
		getView().getButtonLogin().setEnabled(false);
	}

	private void enableButtonLogin() {
		getView().getButtonLogin().setEnabled(true);
	}

	public void handleEvent(IEvent event) {

		if (event.getClass() == ShowLoginViewEvent.class) {
			
			fireEventGlobal(new AddApplicationViewEvent(null, getView()
					.getWidget()));
			
		} else if (event.getClass() == LoginSuccessfulEvent.class) {
			
			loginSuccessful((Long) ((LoginSuccessfulEvent) event).getPayload());
			
		} else if (event.getClass() == LoginEmptyEvent.class) {
			
			loginEmpty();
			
		} else if (event.getClass() == LoginNotFoundEvent.class) {
			
			loginNoFound();
			
		} else if (event.getClass() == PasswordEmptyEvent.class) {
			
			passwordEmpty();
			
		} else if (event.getClass() == PasswordWrongEvent.class) {
			
			passwordWrong();
			
		}

	}

	public void init() {

		buildView();

		register(ShowLoginViewEvent.class);
		
		register(LoginSuccessfulEvent.class);
		register(LoginEmptyEvent.class);
		register(LoginNotFoundEvent.class);

		register(PasswordEmptyEvent.class);
		register(PasswordWrongEvent.class);
	}

	/**
	 * 
	 */
	private void initGridLoginbox() {

		Label labelLogin = new Label(I18N.constants.labelLogin());
		getView().setLabelLogin(labelLogin);

		Label labelPassword = new Label(I18N.constants.labelPassword());
		getView().setLabelPassword(labelPassword);

		TextBox textBoxLogin = new TextBox();
		getView().setTextBoxLogin(textBoxLogin);

		PasswordTextBox passwordTextBox = new PasswordTextBox();
		getView().setPasswordTextBox(passwordTextBox);

		Button buttonLogin = new Button(I18N.constants.buttonLogin());
		buttonLogin.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				loginUser();
			}
		});
		getView().setButtonLogin(buttonLogin);

		Button buttonRegister = new Button(I18N.constants.buttonRegister());
		buttonRegister.addClickListener(new ClickListener() {
			public void onClick(Widget sender) {
				History.newItem(HistoryToken.register);
			}
		});
		getView().setButtonRegister(buttonRegister);

		HorizontalPanel horizontalPanel = new HorizontalPanel();
		horizontalPanel.add(buttonLogin);
		horizontalPanel.add(buttonRegister);

		Grid grid = new Grid(3, 2);

		grid.setWidget(0, 0, labelLogin);
		grid.setWidget(0, 1, textBoxLogin);
		grid.setWidget(1, 0, labelPassword);
		grid.setWidget(1, 1, passwordTextBox);
		grid.setWidget(2, 1, horizontalPanel);

		getView().setGridLoginBox(grid);
	}

	public void initView() {

		Grid gridContainer = new Grid(1, 1);
		gridContainer.setStylePrimaryName("gridLogin");
		getView().setGridContainer(gridContainer);
		
		Label label = new Label(I18N.constants.labelLoginDescription());
		getView().setLabelDescription(label);
		
		initGridLoginbox();

	}
	
	private void loginEmpty() {
		
		getView().getTextBoxLogin().setText("");
		getView().getTextBoxLogin().setText("");
		enableButtonLogin();
		
		Window.alert("The login couldn't be empty");
	}
	
	private void loginNoFound() {
		getView().getTextBoxLogin().setText("");
		getView().getPasswordTextBox().setText("");
		enableButtonLogin();
		
		Window.alert("The Login you entered was'nt found");
	}
	
	private void loginSuccessful(Long sessionId) {

		ASessionAsyncCallback.setSessionId(sessionId);
		History.newItem(HistoryToken.dashboard);
		getComponent().terminateComponent();

	}

	private void loginUser() {

		// TODO login-daten checken

		disableButtonLoging();

		UserLoginDTO userLoginDTO = new UserLoginDTO();
		userLoginDTO.setLogin(getView().getTextBoxLogin().getText());
		userLoginDTO.setPassword(getView().getPasswordTextBox().getText());
		
		fireEventGlobal(new LoginEvent(userLoginDTO, this));

	}
	
	private void passwordEmpty() {
		
		getView().getPasswordTextBox().setText("");
		enableButtonLogin();
		
		Window.alert("You must enter a password");
		
	}

	private void passwordWrong() {
				
		getView().getPasswordTextBox().setText("");
		getView().getButtonLogin().setEnabled(true);
		enableButtonLogin();
		
		Window.alert("The password you entered is not valid for the given login");
	}
	
	public void terminate() {
	}

}
